{--
    Higher rank functions as record fields.

    Compiler aborts with

    > frege.runtime.Undefined: bound Meta not allowed in substTau

    This is due to a wrong assumption on my side, it used to work earlier.
    Should run once compiler is purified.
-}
module tests.comp.Issue55 where

data Flip m a b = F (m b a)

instance Functor (Flip Either a) where
    fmap = undefined

data Rec a  = R { 
    name :: forall r. Functor r => r a -> r String, 
    age  :: a }

getName R{name} x = name x

--- This should be identical to Rec.{zname←}
chgname :: ∀ a  . Rec a    
            → (∀p.Functor p ⇒ (p a → p String) -> (p a → p String)) 
            → Rec a 
chgname r f = case r of
    R g a -> R (f g) a

r1 = R (fmap (const "eins")) 42
r2 = case r1 of
    R o i -> R foo i
r3 = chgname r1 (const foo)


updname :: Rec a  -> Rec a  
updname r = r.{name=fmap (const "yes")} 

foo = fmap (const "foo")

-- cfoo ∷ ∀ t x z. Functor x => z →  (x t → x String)
cfoo = const foo

--idx :: ∀ t p.Functor p => (∀ q. Functor q => q t → q String) → p t → p String 
idx a = a

main = do

    let rec = R (fmap Int.show) 42
        rec2 = rec.{name ← const foo}
        rec3 = rec.{name = fmap (const "yes")}
        -- the following doesn't work because the type variable also appears in the function
        -- rec4 = rec.{age = "acht"}
    println (rec.name  (Just 61))
    println (rec2.name (Just 62))
    println (rec3.name (Just 63))
    -- println rec4.age
    println (getName R{name = fmap  show, age = 42} ([7.45]))
